//
//  CollectGoodsVController.m
//  Experss
//
//  Created by 嗯，大葱 on 17/8/7.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "CollectGoodsVController.h"
#import "CollectGoodCell.h"

#import "CollectGoodsModle.h"
#import "FenLeiDDetailViewController.h"
@interface CollectGoodsVController ()<UITableViewDelegate ,UITableViewDataSource>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArray;

@end

@implementation CollectGoodsVController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _dataArray = [NSMutableArray array];
    [self configureGoodView];
    [self getcollectioGoodsData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)configureGoodView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height-64 -44-10) style:(UITableViewStylePlain)];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = kSpNone;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CollectGoodCell" bundle:nil] forCellReuseIdentifier:@"CollectGoodCell"];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view= [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 10)];
    view.backgroundColor = KShouYeBg;
    return view;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CollectGoodCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CollectGoodCell"];
    cell.selectionStyle = kSeNone;
    cell.cencleBtn.layer.borderWidth = 1.0;
    cell.cencleBtn.layer.borderColor = [KShouYeBg CGColor];
    
    CollectGoodsModle *model = self.dataArray[indexPath.row];
    
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"quetu"]];
    cell.nameLB.text = model.name;
    cell.moneyLB.text = [NSString stringWithFormat:@"¥%@",model.sell_price];
    
    cell.shoucangLB.text = [NSString stringWithFormat:@"%@ 收藏",model.favorite];
    
    [cell.cencleBtn addTarget:self action:@selector(handleCencleCollectgoods:) forControlEvents:(UIControlEventTouchUpInside)];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CollectGoodsModle *model = self.dataArray[indexPath.row];
    FenLeiDDetailViewController *d = [[FenLeiDDetailViewController alloc] init];
    d.goodsID =model.Id;
    
    [self.navigationController pushViewController:d animated:YES];
    
}

//获取收藏商品数据
- (void)getcollectioGoodsData {
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    NSDictionary *parameDic = @{@"userid":saveDic[@"userid"],@"actoken":[Md5Apiway getMD5data:saveDic[@"userid"]]};
    
    [HttpRequest getWithURLString:POST(Shop_PersonalGetGoodsFavorite) parameters:parameDic success:^(id responseObject) {
        if (_dataArray) {
            [_dataArray removeAllObjects];
        }
        
        if ([responseObject[@"ret"] intValue] != 200) {
            [MBProgressHUD showText:@"获取收藏商品出错"];
            return ;
        }
        NSDictionary *tempD = responseObject[@"data"];
        
        if (![[NSString stringWithFormat:@"%@",tempD[@"code"]] isEqualToString:@"0"]) {
            [MBProgressHUD showText:tempD[@"msg"]];
            return;
        }
        
        _dataArray =[CollectGoodsModle mj_objectArrayWithKeyValuesArray:tempD[@"list"]];
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
    
}
- (void)handleCencleCollectgoods:(UIButton *)sender {
    CollectGoodCell *cell = (CollectGoodCell *)[[sender superview] superview];
    NSIndexPath *indePath = [self.tableView indexPathForCell:cell];
    CollectGoodsModle *model = self.dataArray[indePath.row];
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    NSDictionary *parameDic = @{@"userid":saveDic[@"userid"],@"goods_id":model.Id,@"actoken":[Md5Apiway getMD5data:saveDic[@"userid"]]};
    [HttpRequest postWithURLString:POST(Shop_GoodsDetailEditGoodsFavorite) parameters:parameDic success:^(id responseObject) {
        if ([responseObject[@"ret"] intValue] != 200) {
            [[DSLHUD shareDSLHUD] showTextNoActivity:@"操作失败" timeLength:1.5];
            return ;
        }
        [self.view showTextNoActivity:responseObject[@"data"][@"msg"] timeLength:1.5];
        [self getcollectioGoodsData];
        
    } failure:^(NSError *error) {
        
    }];
    
    
    
    
    
}

@end
